import * as categoryApi from '@/api/category.js';
export default {
  namespaced: true,
  state() {
    return {
      // 商品列表
      categoryList: [],
      // 商品二级分类
      subCategoryFilterGoods: [],
      // 商品二级分类懒加载列表
      subCategoryGoods: {
        items: [],
        page: 1,
        pageSize: 10,
        counts: 0,
        pages: 1,
      },
    };
  },
  mutations: {
    // 商品列表
    SET_CATEGORY_LIST(state, newValue) {
      state.categoryList = newValue;
    },
    // 商品二级分类列表
    SET_CATEGORY_FLITER_LIST(state, newValue) {
      state.subCategoryFilterGoods = newValue;
    },
    // 懒加载拼接数据
    SET_SUB_CATEGORY_GOODS(state, newValue) {
      state.subCategoryGoods = {
        ...newValue,
        items: state.subCategoryGoods.items.concat(newValue.items),
      };
    },
    // 懒加载数据重置
    RESET_SUB_CATEGORY_GOODS(state, newValue) {
      state.subCategoryGoods = newValue;
    },
  },
  actions: {
    // 商品分类
    async updateCategoryList({ commit }, params) {
      const res = await categoryApi.getCategory(params);
      commit('SET_CATEGORY_LIST', res);
    },
    // 商品二级分类
    async updateCategoryFilterList({ commit }, id) {
      const res = await categoryApi.getSubCategoryFilterGoods(id);
      commit('SET_CATEGORY_FLITER_LIST', res);
    },
    // 商品二级分类懒加载
    async updateSubCategoryGoods({ commit }, params) {
      const res = await categoryApi.getSubCategoryGoods(params);
      commit('SET_SUB_CATEGORY_GOODS', res);
    },
    async resetSubCategoryGoods({ commit }, params) {
      const res = await categoryApi.getSubCategoryGoods(params);
      commit('RESET_SUB_CATEGORY_GOODS', res);
    },

  },
};
